package com.iss.wyy.helper;

import com.iss.wyy.annotation.Aspect;
import com.iss.wyy.annotation.Controller;
import com.iss.wyy.annotation.Repository;
import com.iss.wyy.annotation.Service;
import com.iss.wyy.util.ClassUtil;
import com.iss.wyy.util.ReflectionUtil;

import java.lang.annotation.Annotation;
import java.util.*;


public class ClassHelper {

    /**
     * 定义类集合（用于存放所加载的类）
     */
    private static final Set<Class<?>> CLASS_SET;
    private static  Set<Class<?>> CONTROLLER_CLASS_SET = new HashSet<>();
    private static  Set<Class<?>> REPOSITORY_CLASS_SET = new HashSet<>();
    private static  Set<Class<?>> SERVICE_CLASS_SET = new HashSet<>();
    private static  Set<Class<?>> ASPECT_CLASS_SET = new HashSet<>();
    private static  Set<Class<?>> ANNOTATION_CLASS_SET = new HashSet<>();

    static {
        //初始化CLASS_SET
        String basePackage = PropertiesHelper.getBasePackage();
        CLASS_SET = ClassUtil.getClassSet(basePackage);

        /**
         * 初始化各个类型的类
         */
        Set<Class<?>> set = new HashSet<>();
        for (Class<?> cls: CLASS_SET) {
            if(cls.isAnnotationPresent(Controller.class)){
                CONTROLLER_CLASS_SET.add(cls);
                ANNOTATION_CLASS_SET.add(cls);
            }else if(cls.isAnnotationPresent(Repository.class)){
                REPOSITORY_CLASS_SET.add(cls);
                ANNOTATION_CLASS_SET.add(cls);
            }else if(cls.isAnnotationPresent(Service.class)){
                SERVICE_CLASS_SET.add(cls);
                ANNOTATION_CLASS_SET.add(cls);
            }else if(cls.isAnnotationPresent(Aspect.class)){
                ASPECT_CLASS_SET.add(cls);
                ANNOTATION_CLASS_SET.add(cls);
            }
        }
    }

    /**
     * 获取所有的Controller类
     * @return
     */
    public static Set<Class<?>> getControllerClassSet() {
        return CONTROLLER_CLASS_SET;
    }
    /**
     * 获取所有的Repository类
     * @return
     */
    public static Set<Class<?>> getRepositoryClassSet() {
        return REPOSITORY_CLASS_SET;
    }
    /**
     * 获取所有的Service类
     * @return
     */
    public static Set<Class<?>> getServiceClassSet() {
        return SERVICE_CLASS_SET;
    }/**
     * 获取所有的Aspect类
     * @return
     */
    public static Set<Class<?>> getAspectClassSet() {
        return ASPECT_CLASS_SET;
    }
    /**
     * 获取所有的类
     * @return
     */
    public static Set<Class<?>> getAnnotationClassSet() {
        return ANNOTATION_CLASS_SET;
    }
    /**
     * 获取应用包名下某父类（或接口）的所有子类（或实现类）
     */
    public static Set<Class<?>> getClassSetBySuper(Class<?> superClass){
        Set<Class<?>> classSet = new HashSet<>();
        for(Class<?> cls:CLASS_SET){
            if(superClass.isAssignableFrom(cls)&&!superClass.equals(cls)){
                classSet.add(cls);
            }
        }
        return classSet;
    }
    /**
     * 获取应用包名下带有某注解的所有类
     */
    public static Set<Class<?>> getClassSetByAnnotation(Class<? extends Annotation> annotationClass){
        Set<Class<?>> classSet = new HashSet<>();
        for(Class<?> cls:CLASS_SET){
            if(cls.isAnnotationPresent(annotationClass)){
                classSet.add(cls);
            }
        }
        return classSet;
    }
}
